package com.team2.outpatient_sys.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.pojo.*;
import com.team2.outpatient_sys.service.OutpatientScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/outpatientSchedule")
public class OutpatientScheduleController {
    @Autowired
    private OutpatientScheduleService outpatientScheduleService;

    /**
     * 处理查询所有排班的请求
     * @return 结果集
     */
    @GetMapping("/{currentPage}/{page}")
    public Result findAllSchedule(@PathVariable Integer currentPage,@PathVariable Integer page) {
        PageHelper.startPage(currentPage, page);

        Result result = new Result();
        List<ScheduleAndDoctor> schedules = outpatientScheduleService.findAllSchedule();

        if (schedules == null || schedules.size() == 0) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            PageInfo<ScheduleAndDoctor> pi = new PageInfo<>(schedules);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pi);
        }
        return result;
    }

    /**
     * 处理按医生姓名查询排班的请求
     * @param name 医生姓名
     * @param currentPage 当前页
     * @param page 每页条数
     * @return 结果集
     */
    @GetMapping("/{name}/{currentPage}/{page}")
    public Result findScheduleByDoctorName(@PathVariable String name,@PathVariable Integer currentPage,@PathVariable Integer page) {
        PageHelper.startPage(currentPage, page);

        Result result = new Result();
        List<ScheduleAndDoctor> schedules = outpatientScheduleService.findScheduleByDoctorName(name);

        if (schedules == null || schedules.size() == 0) {
            result.setCode(500);
            result.setMsg("查询失败，请检查输入的姓名是否有误");
        } else {
            PageInfo<ScheduleAndDoctor> pi = new PageInfo<>(schedules);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pi);
        }
        return result;
    }

    /**
     * 处理单选或批量修改排班医生的请求
     * @param did 医生id
     * @param sIds 排班id
     * @return 结果集
     */
    @PutMapping("/{did}/{sIds}")
    public Result updateSchedule(@PathVariable Integer did,@PathVariable List<Integer> sIds) {
        Result result = new Result();
        Map<String, Integer> map = outpatientScheduleService.updateSchedule(did, sIds);
        //获取失败数
        Integer fail = map.get("fail");

        if (fail == 0) {
            result.setCode(200);
            result.setMsg("总计修改"+map.get("total")+"条，失败"+fail+"条");
        } else {
            result.setCode(500);
            result.setMsg("总计修改"+map.get("total")+"条，失败"+fail+"条");
        }
        return result;
    }

    /**
     * 处理按排班升序查询所有医生的请求
     * @return 结果集
     */
    @GetMapping
    public Result findAllDoctorAsc() {
        Result result = new Result();
        List<DoctorAndNum> doctors = outpatientScheduleService.findAllDoctorAsc();

        if (doctors == null || doctors.size() == 0) {
            result.setCode(500);
            result.setMsg("查询失败，请检查输入的姓名是否有误");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(doctors);
        }
        return result;
    }

    /**
     * 处理查询所有科室筛选器的请求
     * @return 结果集
     */
    @GetMapping("/findAllDeptFilter")
    public Result findAllDeptFilter() {
        Result result = new Result();
        List<DeptFilter> deptFilters = outpatientScheduleService.findAllDeptFilter();

        if (deptFilters == null || deptFilters.size() == 0) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(deptFilters);
        }
        return result;
    }
}
